package com.lost.octopus.es.processor.entity;

import com.lost.octopus.es.helper.constant.ParameterConstant;

import java.util.ArrayList;
import java.util.List;

import lombok.ToString;

@ToString
public class PageIn {

    private int currentPage = ParameterConstant.DEFAULT_CURRENT_PAGE;
    private int linesPerPage = ParameterConstant.DEFAULT_LINES_PER_PAGE;
    private List<OrderBy> orderBys = new ArrayList<>();

    public PageIn() {
    }

    public PageIn(int currentPage, int linesPerPage) {
        this.currentPage = currentPage;
        this.linesPerPage = linesPerPage;
    }

    public List<OrderBy> getOrderBys() {
        return orderBys;
    }

    public void setOrderBys(List<OrderBy> orderBys) {
        this.orderBys = orderBys;
    }

    public int getCurrentPage() {
        if (currentPage <= 0) {
            return ParameterConstant.DEFAULT_CURRENT_PAGE;
        }
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getLinesPerPage() {
        if (linesPerPage <= 0) {
            return ParameterConstant.DEFAULT_LINES_PER_PAGE;
        }
        return linesPerPage;
    }

    public void setLinesPerPage(int linesPerPage) {
        this.linesPerPage = linesPerPage;
    }

    public static PageInBuilder builder() {
        return new PageInBuilder();
    }

    public static class PageInBuilder {
        int currentPage = ParameterConstant.DEFAULT_CURRENT_PAGE;
        int linesPerPage = ParameterConstant.DEFAULT_LINES_PER_PAGE;
        List<OrderBy> orderBys = new ArrayList<>();

        public PageInBuilder currentPage(int currentPage) {
            this.currentPage = currentPage;
            return this;
        }

        public PageInBuilder linesPerPage(int linesPerPage) {
            this.linesPerPage = linesPerPage;
            return this;
        }

        public PageInBuilder orderBys(List<OrderBy> orderBys) {
            this.orderBys = orderBys;
            return this;
        }

        public PageIn build() {
            PageIn pageIn = new PageIn();
            if (linesPerPage <= 0) {
                linesPerPage = ParameterConstant.DEFAULT_LINES_PER_PAGE;
            }
            if (currentPage <= 0) {
                currentPage = ParameterConstant.DEFAULT_CURRENT_PAGE;
            }
            pageIn.setCurrentPage(currentPage);
            pageIn.setLinesPerPage(linesPerPage);
            pageIn.setOrderBys(orderBys);
            return pageIn;
        }
    }
}
